package cn.b.sky.i18n.utils;

import cn.b.sky.i18n.enums.LocaleResourceEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 国际化资源包处理类
 *
 * @author Admin
 */
public class LocaleResourceBundle {

    private static Map<String, LocaleResourceBundle> bundles = new HashMap<String, LocaleResourceBundle>();

    private Map<String, String> resources = new HashMap<String, String>();

    private int i18nSet = 1;

    private LocaleResourceBundle() {
    }

    /**
     * <strong>Title : getBundle<br></strong>
     * <strong>Description : </strong>获取国际化资源<br>
     * <strong>Create on : 2016-6-16 上午9:53:37<br></strong>
     * <p>
     *
     * @param siteCode     工厂
     * @param resourceSet  资源集
     * @param languageCode 语言
     * @return LocaleResourceBundle
     * @throws
     * @author lidapeng<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    public static LocaleResourceBundle getBundle(String siteCode, String resourceSet, String languageCode) {

        String key = resourceSet + "::" + languageCode + "::" + siteCode;
        return bundles.get(key);
    }

    /**
     * <strong>Title : setBundle<br></strong>
     * <strong>Description : </strong>设置bundle<br>
     * <strong>Create on : 2016-5-11 下午1:35:39<br></strong>
     * <p>
     *
     * @param siteCode     工厂
     * @param resourceSet  资源集
     * @param languageCode 语言
     *                     void
     * @throws
     * @author lidapeng<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    public static void setBundle(String siteCode, String resourceSet, String languageCode) {
        if (bundles == null) {
            bundles = new HashMap<String, LocaleResourceBundle>();
        }
        String key = resourceSet + "::" + languageCode + "::" + siteCode;

        if (!bundles.containsKey(key)) {
            bundles.put(key, new LocaleResourceBundle());
        }
    }

    /**
     * <strong>Title : clearBundle<br></strong>
     * <strong>Description : </strong>清除bundle<br>
     * <strong>Create on : 2016-6-28 上午10:35:50<br></strong>
     * <p>
     *
     * @param siteCode
     * @param resourceSet
     * @param languageCode void
     * @throws
     * @author lidapeng<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    public static void clearBundle(String siteCode, String resourceSet, String languageCode) {
        String key = resourceSet + "::" + languageCode + "::" + siteCode;
        if (bundles.containsKey(key)) {
            bundles.remove(key);
        }
    }

    public static String getCommonResourceLabel(String siteCode, String languageCode, String key, String... parameters) {
        return LocaleResourceBundle.getBundle(siteCode, LocaleResourceEnum.COMMONRESOURCES.getValue(), languageCode).getString(key,
                parameters);
    }

    /**
     * 获取国际化资源
     *
     * @param resourceKey 资源键值
     * @param parameters  参数列表
     * @return
     */
    public String getString(String resourceKey, String... parameters) {

        if (StringUtils.isBlank(resourceKey)) {
            return null;
        }
        // 资源尚未定义时，直接返回key值，这里改下找不到就返回null
        if (!resources.containsKey(resourceKey)) {
            //资源未定义提示,如果开启国际化的话提示未定义，没开启的话，直接返回值
            return this.i18nSet == 0 ? resourceKey + " not define!" : resourceKey;
        }

        // 填入可能存在的参数
        StringBuilder result = new StringBuilder(resources.get(resourceKey));
        if (parameters != null) {
            for (int i = 0; i < parameters.length; ++i) {
                String parameterName = "${" + i + "}";
                int indexFound = result.indexOf(parameterName);
                while (indexFound >= 0) {
                    result.replace(indexFound, indexFound + parameterName.length(), parameters[i]);
                    indexFound = result.indexOf(parameterName, indexFound);
                }
            }
        }
        return result.toString();
    }

    public Map<String, String> getResources() {
        return resources;
    }

    public void setResources(Map<String, String> map) {
        this.resources = map;
    }

    public int getI18nSet() {
        return i18nSet;
    }

    public void setI18nSet(int i18nSet) {
        this.i18nSet = i18nSet;
    }

}
